var SysMessage = {
    crt_page:1,
    limit:50
};
//初始化
SysMessage.init = function(){
    this.crt_page = 1;
    this.limit = 50;
    this.page(this.crt_page);
}
SysMessage.page = function(page){
    $.ajax({
        type:'GET',
        url:APP+'/weixin/get.html?limit='+SysMessage.limit+'&page='+page,
        success:function(data){
            var html = '<tr><td colspan="5">无数据</td></tr>'
            if(data!=null){
                SysMessage.crt_page ++;
                var trs = [],
                    total = data['total'],
                    rs = data['rs'];
                SysMessage.dealPage(total);
                if(rs != null){
                    for(var i in rs){
                        var m = rs[i];
                        trs.push('<tr><td>'+
                                (parseInt(i)+1)+'</td><td>'+
                                m.content+'</td><td>'+
                                m.fromUserName+'</td><td>'+
                                m.createTime+'</td><td><span class="button tiny alert" onclick="SysMessage.del(\''+ m.id+'\')">删除</span></td></tr>');
                    }
                }
                if(trs.length>0) html = trs.join('');
            }
            $('#msgTable tbody').html(html);
        },
        error:function(e){
            popup('出错:'+ e);
        }
    });
}
//处理分页
SysMessage.dealPage = function(total){
    var page = ['<li class="arrow '+(SysMessage.crt_page-1<=1?'unavailable':'')+'"><a href="javascript:;">&laquo;</a></li> '],
        pages = (total/SysMessage.limit).toFixed(0);
    for(var i=1;i<pages+2;i++){
        page.push('<li '+(SysMessage.crt_page-1 == i?'class="current"':'')+'>' +
            '<a href="javascript:;" onclick="SysParty.page('+i+')">'+i+'</a>' +
            '</li>');
    }
    page.push('<li class="arrow '+(SysMessage.crt_page-1>=pages?"unavailable":"")+'" ><a href="javacript:;">&raquo;</a></li>');
    $('#msgPage').html(page.join(''));
}
//删除消息
SysMessage.del = function(id)
{
    if(confirm("确认删除此消息？")){
        $.ajax({
            type:'POST',
            url:APP+'/weixin/delete',
            data:'id='+id,
            success:function(data){
                if(data==null){
                    popup('删除消息成功！');
                }else{
                    popup(data);
                }
            },
            error:function(e){
                popup('ERROR:'+ e);
            }
        });
    }
}